import { BeforeInsert, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import * as bcrypt from 'bcrypt';
import { Exclude } from 'class-transformer';

@Entity('user', { comment: '用户' })
export class UserEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ length: 36, comment: '账号' })
  username: string;

  @Exclude()
  @Column({ length: 200, comment: '用户密码' })
  password: string;

  @Column({ length: 32, comment: '用户昵称', default: '' })
  name: string;

  @Column({ type: 'tinyint', comment: '是否超级管理员', default: 0 })
  is_admin: number;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  create_time: Date;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  update_time: Date;

  @Exclude()
  @Column({ type: 'tinyint', default: 0 })
  is_delete: number;

  @BeforeInsert()
  encryptPwd() {
    this.password = bcrypt.hashSync(this.password, 10);
  }
}
